package lolxxs.chenyanting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lolxxs.chenyanting.model.User;
import lolxxs.chenyanting.mapper.UserMapper;
import lolxxs.chenyanting.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lolxxs.chenyanting.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2021-12-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public User login(User user) {
//        Map<String, Object> map = new HashMap();
//        map.put("userName", user.getUserName());
//        List<User> users = userMapper.selectByMap(map);
//        if (users.size() == 1) {
//            User user1 = users.get(0);
//            // 比较密码
//            boolean matches = bCryptPasswordEncoder.matches(user.getPassword(), user1.getPassword());
//            if (matches) {
//                return user1;
//            }
//        }
        return user;
    }

    @Override
    public int add(User user) {
        if (user.getUserName() == null || user.getPassword() == null) {
            return 0;//未填写姓名或密码
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("userName", user.getUserName());
        List<User> list = user.selectList(queryWrapper);
        if (list.size() != 0) {
            return 2;//用户名已经被注册
        }
        user.setUserId(idWorker.nextId());
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        Date date = new Date();
        user.setVersion((long)1);
        user.setCreateTime(date);
        user.setUpdateTime(date);
        user.setState(1);
        user.setIdentity(0);
        return userMapper.insert(user);
    }

    @Override
    public boolean update(User user) {
        if (user.getPassword() != null) {
            user.setUpdateTime(new Date());
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        }
        return userMapper.updateById(user) > 0;
    }

    @Override
    public List<User> query(User user) {
        List<User> users = userMapper.selectList(queryWrapper(user));
        if (users.size() > 0) {
            return users;
        }
        return null;
    }

    @Override
    public int delete(User user) {
        User user1 = new User();
        user1.setState(0);
        user1.setUpdateTime(new Date());
        return userMapper.update(user1, queryWrapper(user));
    }






    private QueryWrapper<User> queryWrapper(User user) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if (user.getUserName() != null) {
            wrapper.eq("userName", user.getUserName());
        }
        if (user.getUserId() != null) {
            wrapper.eq("userId", user.getUserId());
        }
        if (user.getCreateTime() != null) {
            // 大于左边时间
            wrapper.ge("createTime", user.getCreateTime());
        }
        if (user.getUpdateTime() != null) {
            // 小于右边时间
            wrapper.le("createTime", user.getUpdateTime());
        }
        if (user.getState() != null) {
            wrapper.eq("state", user.getState());
        }
        if (user.getIdentity() != null) {
            wrapper.eq("identity", user.getIdentity());//查询投简历者
        }
        if (user.getTelephone() != null) {
            wrapper.eq("telephone", user.getTelephone());
        }
        if (user.getAddress() != null) {
            wrapper.eq("address", user.getAddress());
        }
        if (user.getEmail() != null) {
            wrapper.eq("email", user.getEmail());
        }
        if (user.getSex() != null) {
            wrapper.eq("sex", user.getSex());
        }
        if (user.getOpenid() != null) {
            wrapper.eq("openid", user.getOpenid());
        }
        return wrapper;
    }
}
